<template>
  <div style="padding: 16px;">
    <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:12px;">
      <h3 style="margin:0;">版本管理</h3>
      <el-button type="primary" @click="showPublish = true">发布版本</el-button>
    </div>

    <el-table :data="list" style="width:100%;" size="small" v-loading="loading" empty-text="暂无版本">
      <el-table-column prop="versionTag" label="版本号" width="160" />
      <el-table-column prop="description" label="发布说明" show-overflow-tooltip />
      <el-table-column prop="publishedAt" label="发布时间" width="200">
        <template #default="{ row }">{{ formatDate(row.publishedAt) }}</template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template #default="{ row, $index }">
          <el-button size="small" @click="toCompare(row, $index)">去对比</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div style="margin-top:12px;text-align:right;">
      <el-button @click="load">刷新</el-button>
    </div>

    <el-dialog v-model="showPublish" title="发布版本" width="520px" destroy-on-close>
      <el-form :model="form" label-width="90px">
        <el-form-item label="版本号">
          <el-input v-model="form.versionTag" placeholder="例如：V1.0" maxlength="20" />
        </el-form-item>
        <el-form-item label="发布说明">
          <el-input v-model="form.description" type="textarea" :rows="3" maxlength="500" show-word-limit />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="showPublish = false">取消</el-button>
        <el-button type="primary" :disabled="!canPublish" :loading="publishing" @click="publish">发布</el-button>
      </template>
    </el-dialog>
  </div>
  
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { api } from '@/utils/api'

const route = useRoute()
const router = useRouter()
const projectId = route.params.projectId

const list = ref([])
const loading = ref(false)

const showPublish = ref(false)
const publishing = ref(false)
const form = ref({ versionTag: '', description: '' })
const canPublish = computed(() => !!form.value.versionTag && !!form.value.description)

const formatDate = (iso) => {
  try { return new Date(iso).toLocaleString() } catch { return '-' }
}

const load = async () => {
  loading.value = true
  try {
    list.value = await api.versions.list(projectId)
  } catch (e) {
    ElMessage.error(e.message || '获取版本列表失败')
  } finally {
    loading.value = false
  }
}

const publish = async () => {
  if (!canPublish.value) return
  publishing.value = true
  try {
    await api.versions.publish(projectId, {
      versionTag: form.value.versionTag.trim(),
      description: form.value.description.trim()
    })
    ElMessage.success('发布成功')
    showPublish.value = false
    form.value = { versionTag: '', description: '' }
    await load()
  } catch (e) {
    ElMessage.error(e.message || '发布失败')
  } finally {
    publishing.value = false
  }
}

const toCompare = (row, index) => {
  const base = list.value[index + 1]?._id || list.value[index + 1]?.id || list.value[index - 1]?._id || list.value[index - 1]?.id
  const target = row._id || row.id
  router.push({ path: `/projects/${projectId}/versions/compare`, query: { base, target } })
}

onMounted(load)
</script>




